import Vue from 'vue';
import Vuex from 'vuex';
import {
	axios
} from '../server/index.js'
import $tool from '../tool/index.js'
Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		language: 'en-US',
		isLogin: false,
		userAgent: window.localStorage.getItem('userAgent') || '',
		userinfo: {},
		token: window.localStorage.getItem('token') || '',
		userId: window.localStorage.getItem('userId') || '',
		shopCatNum: 0,
		accountData: {},
		sourceUserId: window.localStorage.getItem('sourceUserId') || '',
	},
	getters: {},
	mutations: {
		SET_USERINFO(store, userinfo) {
			store.userinfo = userinfo
			store.userId = userinfo.id
			window.localStorage.setItem('userId', userinfo.id)
			this.dispatch('get_shop_cat_num')
			if (userinfo.status == 3) {
				this.dispatch('resendthecode')
			}
		},
		SET_TOKEN(store, token) {
			store.token = token
			window.localStorage.setItem('token', token)
		},
		SET_USER_AGENT(store, userAgent) {
			store.userAgent = userAgent
			window.localStorage.setItem('userAgent', userAgent)
		},
		SET_ISLOGIN(store, isLogin) {
			store.isLogin = isLogin
			this.dispatch('userEveryDay')
		},
		SET_LANG(store, language) {
			store.language = language
		},
		SIGN_OUT(store) {
			store.isLogin = false
			store.token = ''
			store.userId = ''
			store.userinfo = {}
			window.localStorage.removeItem('userId')
			window.localStorage.removeItem('token')
		},
		SET_ACCOUNT_MODULE(store, accountData) {
			store.accountData = accountData
			window.localStorage.setItem('accountData', accountData)
		},
		SET_SHOP_CAT_NUM(store, num) {
			store.shopCatNum = num
		},
		SET_sourceUserId(store, id) {
			store.sourceUserId = id
			window.localStorage.setItem('sourceUserId', id)
		},
	},
	actions: {
		get_shop_cat_num() {
			axios({
				url: "user/order/orderCount?status=2"
			}).then(res => {
				this.commit('SET_SHOP_CAT_NUM', res.data)
			})
		},
		userEveryDay() {
			console.log('userEveryDay')
			axios({
				url: 'user/siteUser/firstLoginForDay',
				method: 'post',
				params: {
					time: $tool.timeFormat(new Date().valueOf(), 'yyyy-mm-dd hh:MM:ss')
				}
			})
		},
		resendthecode() {
			axios({
				url: 'user/siteUser/sendActivateEmail',
			}).then(res => {
			})
		},
	},
})
